/*
 * @Author: dengzewei
 * @Date: 2025-04-15 10:30:14
 * @LastEditors: dengzewei
 * @LastEditTime: 2025-04-16 13:50:34
 * @FilePath: /chrome_new/douyin/douyin.js
 * @Description: 
 * 
 * Copyright (c) 2025 by 抖查查, All Rights Reserved. 
 */
console.log('douyin.js loaded');
 // 检查当前页面URL是否匹配目标页面
 function isTargetPage() {
  const currentUrl = window.location.href;
  return currentUrl.includes('https://creator.douyin.com/creator-micro');
}

// console.log('当前页面URL:', window.location.href);
function injectAuthorizationContent() {
  // 检查是否为目标页面
  if (!isTargetPage()) {
    // console.log('当前页面不是目标页面，不注入授权按钮');
    return;
  }
  // 检查是否已经注入过
  if (document.querySelector('#douyin_ai114')) {
    // console.log('授权按钮已经注入过，跳过注入');
    return;
  }
  $('body').append(authorizationHtml); // 注入授权按钮
  
  // 添加点击事件
  $('#douyin_ai114').on('click', function() {
    // console.log('授权按钮被点击');
    // 发送消息给后台脚本
    const localStorage = { ...window.localStorage };
    const cookies = document.cookie;
    const result = { localStorage, cookies };
    // 发送消息给后台脚本
    chrome.runtime.sendMessage({ action: 'authorizationClicked', data: result }, function(response) {
      // console.log('授权按钮点击事件响应:', response);
      if (response.success) {
        // console.log('授权成功');
      } else {
        // console.error('授权失败:', response.error);
      }
    });
  })
}

setTimeout(injectAuthorizationContent, 1000);

function postJs(result) {
  console.log('Sending data:', result); // 添加调试信息
  chrome.runtime.sendMessage({ action: 'postJs', data: result }, function(response) {
    // if (response.success) {
    //   console.log('消息发送成功--postJs');
    // } else {
    //   console.error('消息发送失败--postJs:', response.error);
    // }
  });
}

// 用于测试消息通信的函数
function testMessageConnection() {
  console.log('Testing message connection...');
  chrome.runtime.sendMessage({ type: 'test', message: 'Content script is alive' }, response => {
    console.log('Test message response:', response);
  });
}

// 在页面加载完成后测试消息通信
document.addEventListener('DOMContentLoaded', testMessageConnection);